<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;

/**
 * 交易订单转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m310000_000001_t_vipcard_trade extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * 是否先清理本地数据表
     */
    public $beforeTruncateTargetTable = false;

    /**
     * @inheritdoc
     */
    public function buildQuery() {
        return (new Query())->from('{{%' . $this->sourceTable . '}}')->where(['>', 'id', 73791]);
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'trade' => 'trade_order',
            ],
            'columns' => [
                'user_id' => 'uid',
                'trade_no' => 'tn',
                'paymode',
                'total',
                'cash' => function($data) {
                    return $data['pay'] > 0 ? $data['pay'] : ($data['total'] - $data['credit'] / 100);
                },
                'credit',
                'partner_id' => function($data) {
                    $usage = [
                        'test' => 0,
                        'card' => 1,
                        'annual' => 2,
                        'single' => 3,
                        'shop' => 4,
                        'vip' => 5,
                        'recharge' => 6,
                        'activity' => 7,
                        'ticket2017' => 8,
                        'ticket' => 9,
                    ];
                    return $usage[$data['usage']];
                },
                'status' => function($data) {
                    if ($data['status'] == -9) {
                        return 1;
                    }
                    if ($data['status'] == 1) {
                        return 2;
                    }
                    return $data['status'];
                },
                'unified_no' => 'id',
                'bizno' => function($data) {
                    return 'card2';
                },
                'subject' => function($data) {
                    return $data['mark'] ?: $data['usage'];
                },
                'body' => 'mark',
                'expire_time' => 'expiretime',
                'created_at' => 'inputtime',
                'updated_at' => 'paytime',
                'mark',
            ]
        ];
    }

}
